package org.hank.bluethpbapdemo

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothPbap
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.permissionx.guolindev.PermissionX
import kotlinx.coroutines.launch
import org.hank.bluethpbapdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding
    var pbap: BluetoothPbap? = null

    var myHandler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            println("handle message what == ${msg.what}  data == ${msg.obj}")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.btnConnect.setOnClickListener {
            PermissionX.init(this@MainActivity).permissions(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
            ).request { allgrand, _, _ ->
                if (allgrand) {
                    lifecycleScope.launch() {
                        val bluetoothAdapter: BluetoothAdapter? =
                            BluetoothAdapter.getDefaultAdapter()
                        //mi6  94:87:E0:16:36:E5
                        //redmik60 24:D3:37:66:83:DE
                        val targetDeviceAddress = "24:D3:37:66:83:DE"
                        val targetDevice: BluetoothDevice? =
                            bluetoothAdapter?.getRemoteDevice(targetDeviceAddress)
                    }
                }
            }
        }
    }
}